package cancel.nonpreemptive;

import annotation.ThreadSafe;

import java.util.concurrent.*;

//定义一个CancellingExecutor，继承ThreadPoolExecutor，并重写其newTaskFor方法，使其能够返回自定义任务或原标准任务。
@ThreadSafe
public class CancellingExecutor extends ThreadPoolExecutor {
    public CancellingExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
        if (callable instanceof CancellableTask)
            return ((CancellableTask<T>) callable).newTask();
        else
            return super.newTaskFor(callable);
    }


    /**
     * @param args
     */

    public static void main(String[] args) {
        CancellingExecutor cancellingExecutor = null;
        Future<Object> abc = cancellingExecutor.submit(new SocketUsingTask<Object>() {
            @Override
            public Object call() throws Exception {
                System.out.println("abc");
                return null;
            }
        });
        //取消线程
        abc.cancel(true);
    }
}

